package cn.itcast.bitmapdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.ColorSpace;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class AttributeAnimationActivity extends AppCompatActivity {
    private int screenWidth;
    private ImageView iv_butterfly, iv_bird;
    private AnimationDrawable animation;
    private AnimatorSet flyAnimatorSet;
    private ObjectAnimator objectAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attribute_animation);

        init();
    }

    private void init() {
        getWindowWidth();
        iv_butterfly  = findViewById(R.id.iv_butterfly);
        iv_bird = findViewById(R.id.iv_bird);
        flyAnimation(1);
        flyAnimation(2);
    }

    // 实现飞舞效果
    @SuppressLint("WrongConstant")
    private void flyAnimation(int flag) {
        flyAnimatorSet = new AnimatorSet();//FIXME animator 和animation两个是不一样的
        if (flag==1){
            animation = (AnimationDrawable) iv_butterfly.getBackground();
            objectAnimator = ObjectAnimator.ofFloat(iv_butterfly,
                    "translationX",
                    screenWidth-270);
            objectAnimator.setDuration(3*1000);
        }else{
            animation = (AnimationDrawable) iv_bird.getBackground();
            objectAnimator = ObjectAnimator.ofFloat(iv_bird,
                    "translationX",
                    screenWidth);
            objectAnimator.setRepeatMode(Animation.REVERSE);
            objectAnimator.setRepeatCount(1);
            objectAnimator.setDuration(10*1000);
            objectAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    startActivity(new Intent(AttributeAnimationActivity.this,MainActivity.class));
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }
        objectAnimator.setInterpolator(new LinearInterpolator());// 设置线性插值
        flyAnimatorSet.play(objectAnimator);
        animation.start(); // 启动逐帧动画
        flyAnimatorSet.start(); // 启动属性动画
    }

    // 获取屏幕宽度
    private void getWindowWidth() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
    }
}